/*
    Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef Monster_h__
#define Monster_h__

#include "Interface.h"

#include <SkyGameEngine.h>

#include "Player.h"

class MonsterMachine;

class Monster
    : public IMonster
{
public:
    static void initAudio();

    Monster();
    ~Monster();

    virtual bool init(MonsterType type, Node *parent, IGameLevelScene *g) override;

    //播放走路的动画
    void playMoveAnimation();
    //停止走路的动画
    void stopMoveAnimation();

    //播放攻击动画
    void playAttackAnimation();
    //停止攻击动画
    void stopAttackAnimation();

    //播放受伤的动画
    void playerDamageAnimation();
    //停止受伤的动画
    void stopDamageAnimation();

    //播放死亡动画
    void playDeathAnimation();

    virtual void stand() override;

    virtual void attack(IPlayer *player) override;

    virtual bool canAttack(const Rect &main_player_rect) override;

    virtual void moveTo(const Vector2 &pos) override;

    virtual void run(float dt) override;

    virtual MonsterMachine *getMonsterMachine() override;

    virtual void modifyHp(int dt, ModifyHpType type) override;

    virtual int getCurrentHp() const override;

    virtual bool isDeath() const override;

    virtual Rect getRect() override;

    virtual Vector2 getPosition() override;

    virtual int getMaxHp() const override;

    virtual void setOrder(int order) override;

    virtual void Bruise() override;

    virtual void notifyMiss() override;

protected:
    //初始化动画
    bool initAnimation();
    //初始化Shape
    bool initShape();
    //处理攻击完成
    void onAttackFinish();
    //处理玩家血量变化
    void onHpChange(int old, int now);
    //处理玩家死亡
    void onPlayerDeath();
    //人物发生移动之前
    void onPlayerMoveBefore(Vector2 &pos);
    //人物发生移动之后
    void onPlayerMoveAfter(const Vector2 &old_pos, const Vector2 &now_pos);
    //移动形状

private:
    AnimationSharedPtr m_animationDeath;  //死亡的动画
    AnimationSharedPtr m_animationAttack; //攻击的动画
    AnimationSharedPtr m_animationMove;   //走动的动画
    AnimationSharedPtr m_animationDamage; //受伤的动画

    Sprite *m_playerSp;   //角色的精灵贴图
    Direction m_spDirect; //精灵的朝向 只有 左右两个方向

    Line m_skillLine;  //怪物的技能探测线
    Rect m_playerRect; //怪物的矩形框

    std::unique_ptr<MonsterMachine> m_machine;

    IMonsterDate *m_monsterDate;

    Vector2 m_lastPos; //上一帧的位置

    bool m_isMoving;         //是否正在移动中
    bool m_isAttacking;      //是否正在攻击
    IPlayer *m_attackTarget; //当前的攻击目标

    ProgressBar *m_hpProgressBar; //血条

    MonsterType m_monsterType;

    IGameLevelScene *m_scene;

    bool m_isDeath;

    int m_id;
    bool m_isPlayDamageAnimation;
    int m_hp;

    bool m_isDebug;

    float m_shapeBottom;
    float m_shapeLeft;

    std::wstring m_hurtNumber;
    Label *m_hurtNumberLabel;

    EventListenerHandle m_debugEventHandle;
};

#endif // Monster_h__
